package freaks.terah.gui;

import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JButton;

import freaks.terah.model.Observer;
import freaks.terah.model.PlayerModel;
import freaks.terah.model.Tech;

import javax.swing.JList;
import javax.swing.JTextPane;
import javax.swing.UIManager;

public class TechnologyFrame extends JPanel implements ActionListener, ListSelectionListener, Observer {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JList integrationList;
	private JList newList;
	private JList ownedList;
	private JList researchList;
	private JButton buttonResearch;
	private JButton btnCancel;
	private JButton btnIntegrate;
	private PlayerModel theModel;
	private RessourceCountPanel upkeepPanel;
	private RessourceCountPanel outputPanel;
	private RessourceCountPanel costPanel;
	private JTextPane txtDescription;
	private JLabel txtName;

	/**
	 * Create the panel.
	 */
	public TechnologyFrame(PlayerModel model) {
		setLayout(null);
		theModel = model;
		theModel.addListener(this);
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Edit Technology", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(6, 6, 405, 460);
		add(panel);
		panel.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(21, 53, 154, 151);
		panel.add(scrollPane);
		
		ownedList = new JList();
		ownedList.addListSelectionListener(this);
		ownedList.setModel(model.getOwnedTechs());
		ownedList.setCellRenderer(theModel.getAPRenderer());
		scrollPane.setViewportView(ownedList);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(227, 53, 154, 151);
		panel.add(scrollPane_1);
		
		researchList = new JList();
		researchList.addListSelectionListener(this);
		researchList.setModel(model.getResearchTechs());
		researchList.setCellRenderer(theModel.getAPRenderer());
		scrollPane_1.setViewportView(researchList);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(227, 278, 154, 70);
		panel.add(scrollPane_2);
		
		integrationList = new JList();
		integrationList.addListSelectionListener(this);
		integrationList.setModel(model.getIntegrateTechs());
		integrationList.setCellRenderer(theModel.getRessourceRenderer());
		scrollPane_2.setViewportView(integrationList);
		
		JLabel lblNewLabel = new JLabel("Tech Integration");
		lblNewLabel.setBounds(227, 250, 154, 16);
		panel.add(lblNewLabel);
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		
		JLabel lblNewLabel_1 = new JLabel("Tech Research");
		lblNewLabel_1.setBounds(227, 25, 154, 16);
		panel.add(lblNewLabel_1);
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		
		JScrollPane scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(21, 278, 154, 70);
		panel.add(scrollPane_3);
		
		newList = new JList();
		newList.addListSelectionListener(this);
		newList.setModel(model.getNewTechs());
		newList.setCellRenderer(theModel.getRessourceRenderer());
		scrollPane_3.setViewportView(newList);
		
		JLabel lblNewLabel_2 = new JLabel("New Tech");
		lblNewLabel_2.setBounds(21, 250, 154, 16);
		panel.add(lblNewLabel_2);
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		
		JLabel lblNewLabel_3 = new JLabel("Owned Tech");
		lblNewLabel_3.setBounds(21, 25, 154, 16);
		panel.add(lblNewLabel_3);
		lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
		
		btnIntegrate = new JButton("<-- Integrate");
		btnIntegrate.setBounds(247, 360, 117, 29);
		btnIntegrate.addActionListener(this);
		panel.add(btnIntegrate);
		
		buttonResearch = new JButton("<-- Research");
		buttonResearch.setBounds(247, 216, 117, 29);
		buttonResearch.addActionListener(this);
		panel.add(buttonResearch);
		
		btnCancel = new JButton("Cancel -->");
		btnCancel.setBounds(41, 360, 117, 29);
		btnCancel.addActionListener(this);
		panel.add(btnCancel);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Info", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(419, 6, 285, 460);
		add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblNewLabel_4 = new JLabel("Name:");
		lblNewLabel_4.setHorizontalAlignment(SwingConstants.TRAILING);
		lblNewLabel_4.setBounds(20, 24, 90, 16);
		panel_1.add(lblNewLabel_4);
		
		JLabel lblDescription = new JLabel("Description:");
		lblDescription.setHorizontalAlignment(SwingConstants.TRAILING);
		lblDescription.setBounds(20, 56, 90, 16);
		panel_1.add(lblDescription);
		
		txtDescription = new JTextPane();
		txtDescription.setEditable(false);
		txtDescription.setBackground(UIManager.getColor("Panel.background"));
		txtDescription.setBounds(110, 56, 169, 89);
		panel_1.add(txtDescription);
		
		outputPanel = new RessourceCountPanel();
		outputPanel.setBounds(110, 236, 116, 66);
		panel_1.add(outputPanel);
		
		upkeepPanel = new RessourceCountPanel();
		upkeepPanel.setBounds(110, 314, 116, 66);
		panel_1.add(upkeepPanel);
		
		JLabel lblNewLabel_5 = new JLabel("Cost:");
		lblNewLabel_5.setHorizontalAlignment(SwingConstants.TRAILING);
		lblNewLabel_5.setBounds(20, 184, 90, 16);
		panel_1.add(lblNewLabel_5);
		
		JLabel lblOutput = new JLabel("Output:");
		lblOutput.setHorizontalAlignment(SwingConstants.TRAILING);
		lblOutput.setBounds(20, 262, 90, 16);
		panel_1.add(lblOutput);
		
		JLabel lblUpkeepCost = new JLabel("Upkeep cost:");
		lblUpkeepCost.setHorizontalAlignment(SwingConstants.TRAILING);
		lblUpkeepCost.setBounds(20, 340, 92, 16);
		panel_1.add(lblUpkeepCost);
		
		costPanel = new RessourceCountPanel();
		costPanel.setBounds(110, 158, 116, 66);
		panel_1.add(costPanel);
		
		txtName = new JLabel("");
		txtName.setBounds(110, 24, 169, 16);
		panel_1.add(txtName);
		
		btnCancel.setEnabled(false);
		btnIntegrate.setEnabled(false);
		buttonResearch.setEnabled(false);
	}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		JList list = (JList) arg0.getSource();
		int index = list.getSelectedIndex();
		if (index != -1 && !list.getValueIsAdjusting())
		{
			Tech tech = (Tech) list.getModel().getElementAt(index);
			btnCancel.setEnabled(true);
			btnIntegrate.setEnabled(false);
		    buttonResearch.setEnabled(false);
			
			if (theModel.canBuy(tech))
			{
			    btnIntegrate.setEnabled(true);
			    buttonResearch.setEnabled(true);
			}
			
			if (list != ownedList)
			{
				ownedList.clearSelection();
			}
			if (list != newList)
			{
				newList.clearSelection();
				btnCancel.setEnabled(false);
			}
			if (list != researchList)
			{
				researchList.clearSelection();
				buttonResearch.setEnabled(false);
			}
			if (list != integrationList)
			{
				integrationList.clearSelection();
				btnIntegrate.setEnabled(false);
			}
			
			txtName.setText(tech.getName());
			txtDescription.setText(tech.getDescription());
			
			costPanel.setRessource(tech.getCost(), null);
			outputPanel.setRessource(tech.getOutput(), null);
			upkeepPanel.setRessource(tech.getUpkeep(), null);
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == btnIntegrate && integrationList.getSelectedIndex() != -1)
		{
			theModel.doAction(theModel.ACTION_INTEGRATE_TECH, integrationList.getSelectedIndex());
			
	        btnIntegrate.setEnabled(false);
	        btnCancel.setEnabled(true);
	        newList.setSelectedIndex(theModel.getNewTechs().getSize()-1);
	        
		} else if (arg0.getSource() == buttonResearch && researchList.getSelectedIndex() != -1)
		{
			theModel.doAction(theModel.ACTION_RESEARCH_TECH, researchList.getSelectedIndex());

			buttonResearch.setEnabled(false);
	        btnCancel.setEnabled(true);
	        newList.setSelectedIndex(theModel.getNewTechs().getSize()-1);
		} else if (arg0.getSource() == btnCancel && newList.getSelectedIndex() != -1)
		{
			Tech tech = (Tech) theModel.getNewTechs().get(newList.getSelectedIndex());
			if (tech.getOrigin() == 0)
			{
				theModel.doAction(theModel.ACTION_CANCEL_RESEARCH_TECH, newList.getSelectedIndex());
				buttonResearch.setEnabled(true);
		        btnCancel.setEnabled(false);
		        researchList.setSelectedIndex(theModel.getResearchTechs().getSize()-1);
			}
			if (tech.getOrigin() == 1)
			{
				theModel.doAction(theModel.ACTION_CANCEL_INTEGRATE_TECH, newList.getSelectedIndex());
				
				btnIntegrate.setEnabled(true);
		        btnCancel.setEnabled(false);
		        integrationList.setSelectedIndex(theModel.getIntegrateTechs().getSize()-1);
			}
		}
	}

	@Override
	public void notifyMe() {
		try {
		    if (researchList.getSelectedIndex() != -1)
		    	if (!theModel.canBuy(((Tech)researchList.getSelectedValue())))
			        buttonResearch.setEnabled(false);
		    	else
		    		buttonResearch.setEnabled(true);
		} catch (Exception e)
		{
			buttonResearch.setEnabled(false);
		}
		
		try {
		    if (integrationList.getSelectedIndex() != -1)
		    	if (!theModel.canBuy(((Tech)integrationList.getSelectedValue())))
			        btnIntegrate.setEnabled(false);
		    	else
		    		btnIntegrate.setEnabled(true);
		} catch (Exception e)
		{
			btnIntegrate.setEnabled(false);
		}
	}
}
